package com.example.user_management_p1.controller;
import com.example.user_management_p1.config.CheckCodeRabbitConfig;
import com.example.user_management_p1.service.IUserService;
import com.pojo.util.Result;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    IUserService iUserService;
    @GetMapping("/getCheckcode/{uEmail}")
    public Result<String> getCheckcode(@PathVariable String uEmail){
        rabbitTemplate.convertAndSend(CheckCodeRabbitConfig.EXCHANGE_NAME,
                CheckCodeRabbitConfig.ROUTINGKEY_EMAIL,uEmail);
        return Result.success();
    }
    @GetMapping ("/regiest/{uEmail}/{uPassword}/{CheckCode}")
    public Result<String>regiest(@PathVariable String uEmail,
                                 @PathVariable String uPassword,
                                 @PathVariable String CheckCode){
        return iUserService.insert(uEmail,uPassword,CheckCode)==1?Result.success("1"):Result.failure();
    }
    @PostMapping ("/login")
    public Result<String>login(@RequestParam String uEmail,@RequestParam String uPassword){
        String data=iUserService.login(uEmail,uPassword);
        return data==null?Result.failure():Result.success(data);
    }
    @PostMapping("/updateps")
    public Result<?>updateps(@RequestParam String oldPassword, @RequestParam String newPassword,
                                HttpServletRequest httpServletRequest){

        return iUserService.updatePassword(newPassword,oldPassword,httpServletRequest)==1?Result.success(1):Result.failure();
    }
    @PostMapping ("/updatetp")
    public Result<?>update(@RequestParam Integer uID,@RequestParam Integer uType){
        return iUserService.updateType(uID,uType)==1?Result.success(1):Result.failure();
    }
}
